import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/Layout.vue'

Vue.use(VueRouter)

const routes = [{
        // --- 登录 ---
        path: '/login',
        name: 'Login',
        component: () =>
            import ('@/views/login/Login.vue'),
    },
    {
        path: '/userLogin',
        name: 'userLogin',
        component: () =>
            import ('@/views/login/UserLogin.vue')
    },
    // --- 主页 ---
    {
        path: '/',
        name: 'Layout',
        component: Layout,
        redirect: '/login',
        children: [
            // --- USER ---
            {
                path: 'home',
                name: 'HomeView',
                component: () =>
                    import ('@/views/home/HomeView.vue')
            },
            {
                path: 'userList',
                name: 'UserList',
                component: () =>
                    import ('@/views/user/UserList.vue')
            },
            {
                path: 'addUser',
                name: 'AddUser',
                component: () =>
                    import ('@/views/user/AddUser.vue')
            },
            {
                path: 'editUser',
                name: 'EditUser',
                component: () =>
                    import ('@/views/user/EditUser.vue')
            },
            // --- ADMIN ---
            {
                path: 'adminList',
                name: 'AdminList',
                component: () =>
                    import ('@/views/admin/List.vue'),
            },
            {
                path: 'addAdmin',
                name: 'AddAdmin',
                component: () =>
                    import ('@/views/admin/Add.vue'),
            },
            {
                path: 'editAdmin',
                name: 'EditAdmin',
                component: () =>
                    import ('@/views/admin/Edit.vue'),
            },
            // --- category ---
            {
                path: 'categoryList',
                name: 'CategoryList',
                component: () =>
                    import ('@/views/category/List.vue'),
            },
            {
                path: 'addCategory',
                name: 'AddCategory',
                component: () =>
                    import ('@/views/category/Add.vue'),
            },
            {
                path: 'editCategory',
                name: 'EditCategory',
                component: () =>
                    import ('@/views/category/Edit.vue'),
            },
            { //新增用户个人
                path: 'userCategoryList',
                name: 'userCategoryList',
                component: () =>
                    import ('@/views/category/userCategory.vue'),
            },
            // --- book ---
            {
                path: 'bookList',
                name: 'bookList',
                component: () =>
                    import ('@/views/book/List.vue'),
            },
            {
                path: 'addBook',
                name: 'AddBook',
                component: () =>
                    import ('@/views/book/Add.vue'),
            },
            {
                path: 'editBook',
                name: 'EditBook',
                component: () =>
                    import ('@/views/book/Edit.vue'),
            },
            { //用户新增
                path: 'userBookList',
                name: 'UserBookList',
                component: () =>
                    import ('@/views/book/userBook.vue'),
            },
            // --- borrow ---
            {
                path: 'borrowList',
                name: 'BorrowList',
                component: () =>
                    import ('@/views/borrow/List.vue'),
            },
            {
                path: 'addBorrow',
                name: 'AddBorrow',
                component: () =>
                    import ('@/views/borrow/Add.vue'),
            },
            {
                path: 'editBorrow',
                name: 'EditBorrow',
                component: () =>
                    import ('@/views/borrow/Edit.vue'),
            },
            { //用户新增
                path: 'userBorrowList',
                name: 'UserBorrowList',
                component: () =>
                    import ('@/views/borrow/userBorrow.vue'),
            },
            //---   return ---
            {
                path: 'returnList',
                name: 'ReturnList',
                component: () =>
                    import ('@/views/retur/List.vue'),
            },
            { //用户新增
                path: 'userReturnList',
                name: 'UserReturnList',
                component: () =>
                    import ('@/views/retur/userRetur.vue'),
            },
            //------- message -----------
            {
                path: 'message',
                name: 'Message',
                component: () =>
                      import('@/views/message/Message.vue')
            },
            {
                path: 'addMessage',
                name: 'AddMessage',
                component: () =>
                      import('@/views/message/Add.vue')
            },
            {
                path: 'userMessage',
                name: 'UserMessage',
                component: () =>
                      import('@/views/message/UserMessage.vue')
            }
        ]
    },

    {
        path: "*",
        component: () =>
            import ('@/views/404')
    }

]

const router = new VueRouter({
    mode: 'hash',
    routes
})

// 路由守卫
// router.beforeEach((to, from, next) => {
//     if (to.path === '/login' || to.path === '/userLogin') next()
//     const admin = Cookies.get('admin')
//     if (!admin && to.path !== '/login') {
//         return next('/login') //强制返回到login界面
//     }
//     next('/') //访问/home时，cookie里面存在数据，这个时候就直接放行
// })

export default router